package com.bangdream.michelia.view.fragment.exam.analysis;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bangdream.michelia.R;
import com.bangdream.michelia.contract.ExamContract;
import com.bangdream.michelia.entity.exam.ExamBean;
import com.bangdream.michelia.entity.exam.ExamQuestBean;
import com.bangdream.michelia.entity.exam.ExamResultDetailsBean;
import com.bangdream.michelia.entity.exam.ExamResultItemBean;
import com.bangdream.michelia.entity.exam.SubjectBean;
import com.bangdream.michelia.entity.exam.SubjectInfoBean;
import com.bangdream.michelia.presenter.ExamPresenter;
import com.bangdream.michelia.utils.ImageLoadUtils;
import com.bangdream.michelia.view.activity.exam.OnMainActivityListener;
import com.bangdream.michelia.view.fragment.exam.BaseAnswerFragment;

import java.util.List;

/**
 * @date on 2018/8/21 20:50
 * @email 909732184@qq.com
 * 简答题
 */
public class SolveAnalysis extends BaseAnswerFragment<ExamContract.IExamView,ExamPresenter<ExamContract.IExamView>>
        implements View.OnClickListener,ExamContract.IExamView{
    private View view;
    private TextView btn1,btn2;
    private int nmIndex,nmTotalIndex;
    private TextView tvIndex,tvTotalIndex;
    private TextView tvTitle;
    private ExamResultItemBean itemBean;
    private SubjectInfoBean data;

    //答案解析
    private TextView tvContent,tvUserSelectOnText,tvCorrectText;
    //答案解析的图片
    private ImageView ivPicture;
    private OnMainActivityListener onMainActivityListener;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.answer_analysis_fragment_item_fillblank,container,false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public ExamPresenter createPresenter() {
        return new ExamPresenter();
    }

    @Override
    public void loadData() {
        initView();
        setListener();
        if(itemBean!=null) {
            pd.show();
            mPresenter.getSubjectDetails(itemBean.getId(),"1",null);
        }
    }

    private void setListener() {
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
    }

    private void setViewData() {
        if(data==null)
            return;
        SubjectInfoBean.QuestionInfoListBean question = data.getQuestionInfoList();
        if(question==null)
            return;

        String str ="";
        String type =data.getQuestionType();
        if(type.equals("multiSelection")){ //选择题
            str = "多选题";
        }else if(type.equals("write")){ //填空题
            str = "填空题";
        }else if(type.equals("judgement")){ //判断题
            str = "判断题";
        }else if(type.equals("selection")) { //选择题题
            str = "单选题";
        } else if(type.equals("short")){ //简答题
            str = "简答题";
        }
        str = "("+str+data.getMark()+"分"+")";
        if(question==null)
            return;
        String stTitle = question.getDescription();
//        tvTitle.setText(stTitle+"  "+str);
        tvTitle.setText(Html.fromHtml(stTitle+"  "+"<font color='#2bf0c0'>"+str+"</font>"));
        tvIndex.setText((nmIndex+1)+"");
        tvTotalIndex.setText("/"+nmTotalIndex+"");
        SubjectInfoBean.ThinkingInfoListBean thinkingInfoListBean = data.getThinkingInfoList();
        if(thinkingInfoListBean!=null) {
            tvContent.setText("答案解析：" + thinkingInfoListBean.getDescription());
            ImageLoadUtils.ImageLoad(getActivity(), thinkingInfoListBean.getUri(), ivPicture, -1);
        }
        tvUserSelectOnText.setText(data.getInfoAnswer()+"");
        tvCorrectText.setText("正确答案："+data.getAnswer());
    }

    @Override
    public void setTime(String stTime) {

    }

    @Override
    public void saveAnswer(boolean isFinish, boolean isNext) {

    }

    @Override
    public void setOnMainActivityListener(OnMainActivityListener onMainActivityListener) {
        this.onMainActivityListener = onMainActivityListener;
    }

    private void initView() {
        if(getArguments()!=null){
            nmIndex = getArguments().getInt("index");
            nmTotalIndex = getArguments().getInt("totalIndex");
            itemBean = getArguments().getParcelable("data");
        }
        btn1 = view.findViewById(R.id.btn1);
        btn2 = view.findViewById(R.id.btn2);
        tvUserSelectOnText = view.findViewById(R.id.tvUserSelectOnText);
        tvCorrectText = view.findViewById(R.id.tvCorrectText);
        tvContent = view.findViewById(R.id.tvContent);
        ivPicture = view.findViewById(R.id.ivPicture);
        tvTitle = view.findViewById(R.id.tvTitle);
        tvIndex = view.findViewById(R.id.tvIndex);
        tvTotalIndex = view.findViewById(R.id.tvTotalIndex);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn1:
                if(onMainActivityListener!=null){
                    onMainActivityListener.onPageToLast();
                }
                break;
            case R.id.btn2:
                if(onMainActivityListener!=null){
                    onMainActivityListener.onPageToNext();
                }
                break;
            default:break;
        }
    }

    @Override
    public void setExamList(boolean isSuccess, int page, List<ExamBean> data, String stDescribe) {

    }

    @Override
    public void setSubjectList(boolean isSuccess, List<SubjectBean> data, String stDescribe) {

    }

    @Override
    public void setSubjectDetails(boolean isSuccess, SubjectInfoBean data, String stDescribe) {
        pd.dismiss();
        if(isSuccess){
            this.data = data;
            setViewData();
        }else {

        }
    }

    @Override
    public void initExamId(boolean isSuccess, String examId, String stDescribe) {

    }

    @Override
    public void actionSubmitExam(boolean isSuccess, boolean isFinish, String stDescribe) {

    }

    @Override
    public void actionSubmitQuest(boolean isSuccess, boolean isFinish, String stDescribe) {

    }

    @Override
    public void setExamResultList(boolean isSuccess, List<ExamResultItemBean> data, String stDescribe) {

    }

    @Override
    public void setExamResultDetails(boolean isSuccess, ExamResultDetailsBean data, String stDescribe) {

    }

    @Override
    public void setExamQuestList(boolean isSuccess, List<ExamQuestBean> data, String stDescribe) {

    }

    @Override
    public void setExamQuList(boolean isSuccess, List<ExamBean> data, String stDescribe) {

    }
}
